define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cyclopedia/hospital/index' + location.search,
                    add_url: 'cyclopedia/hospital/add',
                    edit_url: 'cyclopedia/hospital/edit',
                    del_url: 'cyclopedia/hospital/del',
                    multi_url: 'cyclopedia/hospital/multi',
                    import_url: 'cyclopedia/hospital/import',
                    his_off:'hisoffice/index',
                    table: 'hospital',

                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weight',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'tag', title: __('标签'), operate: false, formatter: Table.api.formatter.flag,searchable:false},
                        // {field: 'address', title: __('Address'), operate: 'LIKE'},
                        // {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        // {field: 'content', title: __('Content')},
                        {field: 'region.name', title:'区域',operate: 'false',},

                        {field: 'offices', title: __('科室'), operate: false,formatter: Table.api.formatter.flag,searchable:false},
                        // {field: 'office', title: "设置病症",formatter:function(value, row){
                        //         return '<button type="button" data-id="'+row.id+'" class="btn btn-primary house_file" data-toggle="button"> 设置病症</button>';
                        //     }},
                        // {
                        //     field: 'office',
                        //     width: "120px",
                        //     title: __('试题'),
                        //     table: table,
                        //     events: Table.api.events.operate,
                        //     buttons: [
                        //         {
                        //             name: 'his_off',
                        //             text: __('病症设置'),
                        //             title: __('病症设置'),
                        //             classname: 'btn btn-xs btn-primary btn-dialog',
                        //             icon: 'fa fa-list',
                        //             url: "hisoffice/index?id={office}",
                        //             callback: function (data) {
                        //                 Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                        //             },
                        //             visible: function (row) {
                        //                 //返回true时按钮显示,返回false隐藏
                        //                 return true;
                        //             }
                        //         },
                        //     ],
                        //     formatter: Table.api.formatter.buttons
                        // },
                        {
                            field: 'weight',
                            title: '排序',
                            formatter: function (value, row, index) {
                                return '<input type="text" class="form-control text-center text-weigh" data-id="' + row.id + '" value="' + value + '" style="width:50px;margin:0 auto;" />';
                            },
                            events: {
                                "dblclick .text-weigh": function (e) {
                                    e.preventDefault();
                                    e.stopPropagation();
                                    return false;
                                }
                            }
                        },
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('医院状态'),yes:1,no:0, formatter: Table.api.formatter.toggle},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                        //
                        //     }
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            // buttons: [
                            //     {
                            //         name: 'detail',
                            //         text: __('科室管理'),
                            //         icon: 'fa fa-commenting',
                            //         classname: 'btn btn-primary btn-xs btn-rec btn-dialog',
                            //         url: 'hisoffice/index'+'?office={office}'+'&hos_id={id}'
                            //     },]
                        }
                    ]
                ]
            });
            $(document).on('click','.house_type',function(){
                var id = $(this).attr('data-id');
                var url = 'hisoffice/index?office_ids='+id;
                Fast.api.open(url, __('Add'),{});
            });
            $(document).on("change", ".text-weigh", function () {
                $(this).data("params", {weight: $(this).val()});
                Table.api.multi('', [$(this).data("id")], table, this);
                return false;
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'cyclopedia/hospital/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'cyclopedia/hospital/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'cyclopedia/hospital/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
